//
//  MainVC.swift
//  Example
//
//  Created by remy on 2019/3/25.
//  Copyright © 2019 com.Risac. All rights reserved.
//

import UIKit

class MainVC: BaseVC {
    private lazy var pack: TablePack = TablePack(frame: self.bodyViewFrame, style: .grouped) {
        $0.view.rowHeight = 44.0
        // MARK: UITableView.style为.grouped时使用代理方式设置sectionHeader/sectionFooter的高度
        // MARK: 直接设置UITableView.sectionHeaderHeight/UITableView.sectionFooterHeight会有问题
        $0.action.heightCalculate = [.sectionHeader]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        topBarView.centerItem = .text("Risac")
        topBarView.rightItems = [.text("变换") {
            $0.margin.right = 16.0
            $0.action(UITapGestureRecognizer(target: self, action: #selector(self.reloadTap)))
        }]
        let arr1 = ["Loader", "Collection Flex", "Collection Waterfall", "Pop", "String", "component"]
        let arr2 = ["NestScrollView"]
        pack.action.handle(.tap, cellItemClass: MainItem.self) { [weak self] in
            guard let self = self else { return }
            var vc: BaseVC
            if $1.section == 0 {
                switch $1.row {
                case 0:
                    vc = LoaderVC()
                case 1:
                    vc = CollectionFlexLayoutVC()
                case 2:
                    vc = CollectionFlowLayoutVC()
                case 3:
                    vc = PopVC()
                case 4:
                    vc = StringVC()
                case 5:
                    vc = ComponentVC()
                default:
                    return
                }
                vc.title = arr1[$1.row]
            } else {
                switch $1.row {
                case 0:
                    vc = NestScrollViewVC()
                default:
                    return
                }
                vc.title = arr2[$1.row]
            }
            self.pushVC(vc)
        }
        let sectionItem1 = MainSectionItem("Risac")
        sectionItem1.cellItems = arr1.map({ MainItem($0) })
        let sectionItem2 = MainSectionItem("Example")
        sectionItem2.cellItems = arr2.map({ MainItem($0) })
        pack.model.set([sectionItem1, sectionItem2])
        self.view.addSubview(pack.view)
        pack.view.reloadData()
    }
    
    @objc private func reloadTap() {
        topBarView.centerItem = .text("Risac") {
            $0.bgColor = FlatColor.randomColor()
            $0.textColor = FlatColor.midnightBlue
            let num = (0..<4).map { _ in CGFloat(arc4random() % 11) }
            $0.inset = UIEdgeInsets(top: 0.0, left: num[0], bottom: 0.0, right: num[1])
            $0.margin = UIEdgeInsets(top: num[2], left: 0.0, bottom: num[3], right: 0.0)
            $0.font = UIFont.boldSystemFont(ofSize: CGFloat(arc4random() % 5 + 12))
        }
    }
}

class MainSectionItem: TableSectionItem {
    let text: String
    
    init(_ text: String) {
        self.text = text
        super.init()
        sectionHeaderClass = MainSection.self
        sectionHeaderHeight = 32.0
    }
    
    override func reload() {}
}

class MainSection: TableSection {
    private lazy var titleLabel: UILabel = UILabel(textColor: FlatColor.wetAsphalt, fontSize: 16.0, bold: true)
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = FlatColor.turquoise
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(16.0)
            make.right.equalTo(-16.0)
            make.centerY.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func updateSection(sectionItem: TableSectionItem?) {
        guard let item = sectionItem as? MainSectionItem else { return }
        titleLabel.text = item.text
    }
}

class MainItem: TableCellItem {
    let text: String
    
    init(_ text: String) {
        self.text = text
        super.init()
        cellClass = MainCell.self
    }
    
    override func reload() {}
}

class MainCell: TableCell {
    private lazy var titleLabel: UILabel = UILabel(textColor: FlatColor.midnightBlue, fontSize: 14.0)
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = .white
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(16.0)
            make.right.equalTo(-16.0)
            make.centerY.equalToSuperview()
        }
        let bottomLine = UIView(frame: CGRect(x: 0.0, y: contentView.height - Size.onePixel, width: contentView.width, height: Size.onePixel), bgColor: FlatColor.silver)
        bottomLine.autoresizingMask = [.flexibleWidth, .flexibleTopMargin]
        contentView.addSubview(bottomLine)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func updateCell(cellItem: TableCellItem?) {
        guard let item = cellItem as? MainItem else { return }
        titleLabel.text = item.text
    }
}
